#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ == taoyulong2018@gmail.com
# __time__ == 2023/9/20 9:44
# ===========================================
#       题目名称： 58. 最后一个单词的长度
#       题目地址： https://leetcode.cn/problems/length-of-last-word/
#       题目描述： https://note.youdao.com/s/VjIUrvCD
# ===========================================


class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        stack = list(s)
        count = 0
        while stack:
            target = stack.pop()
            if count > 0 and target.isspace():      # 判断是否有空格
                break
            if target.isspace():
                continue
            count += 1
        return count


if __name__ == '__main__':
    s = Solution()
    # 5
    print(s.lengthOfLastWord(s="Hello World"))
    # 4
    print(s.lengthOfLastWord(s="   fly me   to   the moon  "))
    # 6
    print(s.lengthOfLastWord(s="luffy is still joyboy"))
